// myorderdetail.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp()
var goodstimer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    shihan.sendRequest(config.get_order_detail, 'POST', {
        order_no: options.orderid
      })
      .then(function(response) { //sh_create_at
        // console.log(response);
        if (response.data.code == 0) {
          that.setData({
            showLoading: false,
            order_no: options.orderid,
            order_detail: response.data.data,
          })
        }
        clearInterval(goodstimer);
        goodstimer = setInterval(function() {
          that.dateformat(that.data.order_detail.sh_create_at);
        }, 1000);
      }, function(error) {
        console.log(error);
      });
  },
  aftersalebtn: function(e) {
    if (this.data.order_detail.sh_is_offline) {
      shihan.showToast.call(this, this.data.order_detail.sh_no_refund_label);
    } else {
      var word;
      var that = this
      var status = e.currentTarget.dataset.status
      var ordetype = e.currentTarget.dataset.ordetype
      if (status >= 1) {
        word = '请在世瀚百年APP内查看售后进度（使用微信登录）'
      } else {
        word = '请在世瀚百年APP内申请售后服务（使用微信登录）'
      }
      if (ordetype == 1) {
        this.setData({
          showCancel: false
        })
        word = '该商品无法申请退货，只支持换货。了解详情，请咨询客服。'
      } else {
        this.setData({
          showCancel: true
        })
      }
      wx.showModal({
        title: "提示",
        content: word,
        confirmText: "确认",
        confirmColor: "#c40000",
        showCancel: that.data.showCancel,
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(config.commonmoburl + 'other/downloadapp.html')
            })
          }
        }
      })
    }
  },
  openstoredetail: function(e) {
    // if (e.currentTarget.dataset.ordetype == 0){
    wx.navigateTo({
      url: '/pages/storedetail/storedetail?id=' + e.currentTarget.dataset.id
    })
    // }
  },
  opengoodsdetail: function(e) {
    var ordetype = e.currentTarget.dataset.ordetype
    if (ordetype != 1) {
      wx.navigateTo({
        url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
      })
    }
  },
  dateformat: function(staretime) { //sh_create_at:"2017-08-28 16:23:26"
    var that = this
    var time1 = new Date().getTime();
    var time2 = new Date(staretime.replace(/-/g, '/'));

    var micro_second = 1800000 - (time1 - time2);

    if (micro_second >= 0) {
      var time = micro_second / 1000;
      // 分钟位
      var minute = Math.floor(time / 60);
      minute = minute <= 9 ? '0' + minute : minute;
      // 秒数
      var second = Math.floor(time % 60);
      second = second <= 9 ? '0' + second : second;
      that.setData({
        changetime: {
          status: true,
          changeMinute: minute,
          changeSecond: second
        }
      })
    } else {
      clearInterval(goodstimer);
      shihan.sendRequest(config.get_order_detail, 'POST', {
          order_no: that.data.order_no
        })
        .then(function(response) { //sh_create_at
          that.setData({
            order_detail: response.data.data
          })
        }, function(error) {
          console.log(error);
        });
    }
  },
  copyContent: function (e) {
    var that = this
    wx.setClipboardData({
      data: String(e.currentTarget.dataset.content),
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            if (res.data) {
              console.log('复制成功')
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(goodstimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(goodstimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this
    shihan.sendRequest(config.get_order_detail, 'POST', {
        order_no: that.data.order_no
      })
      .then(function(response) { //sh_create_at
        that.setData({
          order_detail: response.data.data
        })
        wx.stopPullDownRefresh()
      }, function(error) {
        console.log(error);
      });
  }
})